package com.xmy.manager;


import com.xmy.entity.Product;
import com.xmy.entity.Sale;
import com.xmy.entity.User;
import com.xmy.service.ProductService;
import com.xmy.service.SaleService;
import com.xmy.service.UserService;
import com.xmy.service.impl.ProductServiceImpl;
import com.xmy.service.impl.SaleServiceImpl;
import com.xmy.service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ShopManager {
    Scanner input = new Scanner(System.in);
    ProductService ps = new ProductServiceImpl();
    UserService us = new UserServiceImpl();
    SaleService ss = new SaleServiceImpl();

    /**
     * 主菜单
     */
    public void menu(){
        System.out.println("---欢迎使用小型进销存系统---");
        System.out.println("请登录");
        System.out.print("用户名：");
        String userName = input.next();
        System.out.print("密码：");
        String pwd = input.next();
        do {
            User user = us.selUser(userName,pwd);
            if (user!=null){
                System.out.println("欢迎您！"+user.getRealName());
                break;
            }else{
                System.out.println("登录失败！请重新输入：");
                System.out.print("用户名：");
                userName = input.next();
                System.out.print("密码：");
                pwd = input.next();
            }
        } while (true);
        do {
            System.out.println("1.查询所有商品");
            System.out.println("2.查询指定商品的库存");
            System.out.println("3.修改指定商品的库存");
            System.out.println("4.添加销售记录");
            System.out.println("5.对销售记录按指定的排序规则分页查询");
            System.out.println("6.退出");
            System.out.print("请选择：");
            int choose = input.nextInt();
            switch (choose){
                case 1:
                    selAllProduct();
                    break;
                case 2:
                    System.out.print("请输入商品名称：");
                    String name = input.next();
                    selProductByName(name);
                    break;
                case 3:
                    System.out.print("请输入商品名称：");
                    String name1 = input.next();
                    System.out.print("请输入修改的数量：");
                    int count = input.nextInt();
                    updateProductByName(name1,count);
                    break;
                case 4:
                    Sale sale = new Sale();
                    System.out.print("请输入商品价格：");
                    sale.setPrice(input.nextDouble());
                    System.out.print("请输入商品数量：");
                    sale.setQuantity(input.nextInt());
                    System.out.println("商品总价为："+(sale.getPrice()*sale.getQuantity()));
                    sale.setTotalPrice(sale.getPrice()*sale.getQuantity());
                    System.out.print("请输入用户编号：");
                    sale.setUserId(input.nextInt());
                    System.out.print("请输入商品编号：");
                    sale.setProductId(input.nextInt());
                    addSaleInfo(sale);
                    break;
                case 5:
                    System.out.print("请输入每页的条数：");
                    int size = input.nextInt();
                    System.out.print("总页数是："+(ss.selCount()/size));
                    System.out.print("请输入您要查看的页数：");
                    int page = input.nextInt();
                    selSaleLimit(page,size);
                    break;
                case 6:
                    System.out.println("系统退出!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("请输入正确的选项！");
                    break;
            }
            System.out.println();
        } while (true);
    }

    /**
     * 新增销售记录
     * @param sale
     */
    private void addSaleInfo(Sale sale) {
        ss.addSaleInfo(sale);
        System.out.println("新增成功！");
    }

    /**
     * 修改商品库存
     * @param name1
     */
    private void updateProductByName(String name1,int count) {
        ps.updateProductCount(name1,count);
        System.out.println("修改成功！");
    }

    /**
     * 查询全部商品
     */
    public void selAllProduct(){
        System.out.println("编号\t商品名称\t库存");
        List<Product> products = ps.selAllProduct();
        for (Product p:products) {
            System.out.println(p.getId()+"\t"+p.getProductName()+"\t"+p.getQuantity());
        }
    }

    /**
     * 查询指定商品库存
     * @param name
     */
    public void selProductByName(String name){
        Product product = ps.selProductCount(name);
        if (product!=null){
            System.out.println("商品名称\t库存");
            System.out.println(product.getProductName()+"\t"+product.getQuantity());
        }else{
            System.out.println("不存在该商品！");
        }
    }

    /**
     * 分页查询
     */
    public void selSaleLimit(int page,int size){
        List<Sale> sales = ss.selAllSale(page,size);
        System.out.println("编号\t价格\t数量\t总价\t销售日期\t销售员id\t商品id");
        for (Sale sale: sales) {
            System.out.println(sale.getId()+"\t"+sale.getPrice()+"\t"+sale.getQuantity()+
                    "\t"+sale.getTotalPrice()+"\t"+sale.getTime()+"\t"+sale.getUserId()+"\t"+sale.getProductId());
        }
    }
}
